package com.compala.hajniapp;

import java.util.Random;

import android.graphics.drawable.Drawable;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageButton;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
    private enum TapAction {
        KISS,HAND
    }
    private Random random;
    private TypedArray goodGuys ;
    private TypedArray badGuys ;
    private TypedArray smileys ;
    private int score;
    private int pictureid;
    private ImageView smiley;
    private ImageButton kissButton;
    private ImageButton handButton;
    private ImageButton guyToAct;
    private boolean timetoquit;

    private TapAction tapAction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Resources res = getResources();
        super.onCreate(savedInstanceState);
        goodGuys = res.obtainTypedArray(R.array.Goodguys);
        badGuys = res.obtainTypedArray(R.array.Badguys);
        smileys = res.obtainTypedArray(R.array.Smileys);
        random = new Random();
        tapAction=TapAction.KISS;
        setContentView(R.layout.activity_main_activity);
        kissButton = (ImageButton) findViewById(R.id.KissButton);
        kissButton.setBackgroundResource(R.color.white);
        handButton = (ImageButton) findViewById(R.id.HandButton);
        guyToAct = (ImageButton) findViewById(R.id.GuyToAct);
        smiley = (ImageView) findViewById(R.id.Smiley);
        getActionBar().hide();
        pictureid = 1;
        guyToAct.setImageDrawable(getGuy(pictureid));
        this.score = 2;
        smiley.setImageDrawable(smileys.getDrawable(this.score));
        timetoquit=false;
    }

    public void onKissTaped(View view) {
        kissButton.setBackgroundResource(R.color.white);
        handButton.setBackgroundResource(R.color.transparent);
        /*DEBUG
        Toast.makeText(MainActivity.this,
            "onKissTaped is running!", Toast.LENGTH_SHORT).show();
        /* */
        setTapAction(TapAction.KISS);
    }
    public void onHandTaped(View view) {
        kissButton.setBackgroundResource(R.color.transparent);
        handButton.setBackgroundResource(R.color.white);
        /*DEBUG
        Toast.makeText(MainActivity.this,
            "onHandTaped is running!", Toast.LENGTH_SHORT).show();
        /* */
        setTapAction(TapAction.HAND);
    }
    public void onGuyToActClicked(View view) {
        /*DEBUG
        Toast.makeText(MainActivity.this,
            "onGuyToActClicked is running!", Toast.LENGTH_SHORT).show();

        /* */
        if (timetoquit) MainActivity.this.finish();
        boolean right=!((pictureid<badGuys.length() && tapAction==TapAction.KISS) 
                    || (pictureid>=badGuys.length() && tapAction==TapAction.HAND));
        if (right) {
            this.score+=1;            
        }
        else{
            this.score-=1;
        }
        if (this.score<0 ){
            Toast.makeText(MainActivity.this,
                "Ne Máá!!", Toast.LENGTH_SHORT).show();
            this.score+=1;
        }
        if (this.score>=smileys.length()){
            Toast.makeText(MainActivity.this,
                ":) Úgy imádlak! :)" ,Toast.LENGTH_SHORT).show();
            this.score-=1;   
            timetoquit =true;
            guyToAct.setImageDrawable(goodGuys.getDrawable(0));
            return;
        }
        else{
            pictureid=random.nextInt(badGuys.length()+goodGuys.length());
            /*DEBUG
            if (getGuy(pictureid)!= null)
            /* */
            guyToAct.setImageDrawable(getGuy(pictureid));
            /*DEBUG
            else{
                Toast.makeText(MainActivity.this,
                    "getGuy("+pictureid+"): returned null.", Toast.LENGTH_SHORT).show();
            }
            /* */
            /*DEBUG
            if (smileys.getDrawable(this.score)!= null)
            /* */
            smiley.setImageDrawable(smileys.getDrawable(this.score));
            /*DEBUG
            else{
                Toast.makeText(MainActivity.this,
                    "setImageDrawable("+this.score+") returned null.", Toast.LENGTH_SHORT).show();
            }
            /* */
        }
    }
    private void setTapAction(TapAction tapAction) {
        this.tapAction=tapAction;
    }
    private Drawable getGuy(int i){
        if (i<badGuys.length()){
            return badGuys.getDrawable(i);
        }
        else{
            i -= badGuys.length();
            if (i<goodGuys.length()){
                return goodGuys.getDrawable(i);
            }
        }
        
        return null;
    }

}
